package com.example.mys_app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    tab1 fragment1;
    tab2 fragment2;
    tab3 fragment3;
    tab4 fragment4;
    FragmentManager fm;

    LinearLayout LinearLayout1,LinearLayout2,LinearLayout3,LinearLayout4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        fragment1=new tab1();
        fragment2=new tab2();
        fragment3=new tab3();
        fragment4=new tab4();

        LinearLayout1=findViewById(R.id.LinearLayout1);
        LinearLayout2=findViewById(R.id.LinearLayout2);
        LinearLayout3=findViewById(R.id.LinearLayout3);
        LinearLayout4=findViewById(R.id.LinearLayout4);

        fm=getSupportFragmentManager();

        initial();

        fragmenthide();
        fragmentshow(fragment1);

        LinearLayout1.setOnClickListener(this);
        LinearLayout2.setOnClickListener(this);
        LinearLayout3.setOnClickListener(this);
        LinearLayout4.setOnClickListener(this);

    }
        private void initial(){

            FragmentTransaction ft=fm.beginTransaction()
                    .add(R.id.content1,fragment1)
                    .add(R.id.content1,fragment2)
                    .add(R.id.content1,fragment3)
                    .add(R.id.content1,fragment4);

            ft.commit();
        }

        public void onClick(View view){
            fragmenthide();
            int id = view.getId();
            if (id == R.id.LinearLayout1) {
                fragmentshow(fragment1);
            } else if (id == R.id.LinearLayout2) {
                fragmentshow(fragment2);
            } else if (id == R.id.LinearLayout3) {
                fragmentshow(fragment3);
            } else if (id == R.id.LinearLayout4) {
                fragmentshow(fragment4);
            }

        }
        private void fragmenthide(){
            FragmentTransaction ft=fm.beginTransaction()
                    .hide(fragment1)
                    .hide(fragment2)
                    .hide(fragment3)
                    .hide(fragment4);

            ft.commit();

        }

        private void fragmentshow(Fragment fragment){
            FragmentTransaction transaction=fm.beginTransaction()
                    .show(fragment);
            transaction.commit();

        }
}